﻿using System.Linq;

namespace HEFramework
{
    public static class LegacySystem
    {
        [EntitySystem]
        public class LegacyAwakeSystem : AwakeSystem<Legacy>
        {
            protected override void Awake(Legacy _self)
            {
                var dc = _self.GetSceneChild<DataTableComponent>();
                if (dc == null)
                {
                    return;
                }

                var legacy_table = dc.LoadDataTable<DRLegacy>();
                var legacy_datas = legacy_table.GetAllData();
                for (int i = 0; i < legacy_datas.Length; i++)
                {
                    var data = legacy_datas[i];
                    if (data.IsFind == 1)
                    {
                        _self.LegacyFinds.Add(data.ID);
                    }
                    else
                    {
                        _self.Legacies.Add(data.ID);
                    }
                }
            }
        }

        public static void FindLegacy(this Legacy _self, int _id)
        {
            if (_self.IsFind(_id))
            {
                return;
            }

            _self.Legacies.Remove(_id);
            _self.LegacyFinds.Add(_id);

            Log.Debug("【发现】遗物ID ->" + _id);
        }

        public static int AddFindLegacy(this Legacy _self, int _id)
        {
            if (_self.IsMax(_id))
            {
                return (int)eLegacyError.OutLimit;
            }

            if (!_self.IsFind(_id))
            {
                return (int)eLegacyError.System;
            }

            if (_self.IsHave(_id))
            {
                return (int)eLegacyError.System;
            }


            _self.LegacyFinds.Remove(_id);
            return _self.AddLegacy(_id);
        }

        public static int AddLegacy(this Legacy _self, int _id)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            var reward = _self.GetSceneChild<Reward>();
            var element = _self.GetSceneChild<Element>();
            if (dc == null || reward == null)
            {
                return (int)eLegacyError.System;
            }

            if (_self.IsMax(_id))
            {
                return (int)eLegacyError.OutLimit;
            }

            if (_self.IsHave(_id))
            {
                return (int)eLegacyError.System;
            }

            var data = dc.LoadDataTable<DRLegacy>().GetData(_id);
            //更新属性
            for (int i = 0; i < data.RewardIDs.Length; i++)
            {
                if (data.RewardIDs[i] == -1)
                {
                    continue;
                }

                reward.ChangeTeamReward(data.RewardIDs[i], data.RewardValues[i]);
            }

            //更新五行
            for (int i = 0; i < data.ElementIDs.Length; i++)
            {
                if (data.ElementIDs[i] == -1)
                {
                    continue;
                }

                element.ChangeElement(data.ElementIDs[i]);
            }


            if (_self.IsPlace(data.ID))
            {
                _self.LegacyPlaces.Add(data.ID);
            }
            else
            {
                _self.LegacyUnPlaces.Add(data.ID);
            }

            Log.Debug("【添加】遗物ID ->" + data.ID);

            _self.SearchLegacy(_id);
            _self.ComposeLegacy();
            return (int)eLegacyError.None;
        }

        public static int RemoveLegacy(this Legacy _self, int _id)
        {
            if (!_self.IsHave(_id))
            {
                return (int)eLegacyError.System;
            }

            if (_self.LegacyPlaces.Contains(_id))
            {
                _self.LegacyPlaces.Remove(_id);
                Log.Debug("【移除】遗物ID ->" + _id);
                return (int)eLegacyError.None;
            }

            if (_self.LegacyUnPlaces.Contains(_id))
            {
                _self.LegacyUnPlaces.Remove(_id);
                Log.Debug("【移除】遗物ID ->" + _id);
                return (int)eLegacyError.None;
            }

            return (int)eLegacyError.System;
        }

        public static void SearchLegacy(this Legacy _self, int _id)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return;
            }

            var legacy_table = dc.LoadDataTable<DRLegacy>();
            var legacy_data = legacy_table.GetData(_id);
            for (int i = 0; i < legacy_data.SearchIDs.Length; i++)
            {
                int id = legacy_data.SearchIDs[i];
                if (id == -1)
                {
                    continue;
                }

                _self.FindLegacy(id);
            }
        }

        public static void ComposeLegacy(this Legacy _self)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return;
            }


            var legacy_table = dc.LoadDataTable<DRLegacy>();
            //搜索所有能合物体
            using ListObject<int> composes = ListObject<int>.Create();
            for (int i = 0; i < _self.Legacies.Count; i++)
            {
                if (!_self.IsCompose(_self.Legacies[i]))
                {
                    continue;
                }

                composes.Add(_self.Legacies[i]);
            }

            using ListObject<int> final = ListObject<int>.Create();
            //检测配件是否足够
            for (int i = 0; i < composes.Count; i++)
            {
                var legacy_data = legacy_table.GetData(composes[i]);
                bool is_compose = true;
                //检测可合成物体
                for (int j = 0; j < legacy_data.ComposeIDs.Length; j++)
                {
                    if (!_self.IsHave(legacy_data.ComposeIDs[j]))
                    {
                        is_compose = false;
                        break;
                    }
                }

                if (is_compose)
                {
                    //移除配件
                    for (int j = 0; j < legacy_data.ComposeIDs.Length; j++)
                    {
                        _self.RemoveLegacy(legacy_data.ComposeIDs[j]);
                    }

                    final.Add(composes[i]);
                }
            }

            //最后统一合成
            for (int i = 0; i < final.Count; i++)
            {
                _self.Legacies.Remove(final[i]);
                _self.AddLegacy(final[i]);
            }
        }

        public static bool IsFind(this Legacy _self, int _id)
        {
            if (_self.Legacies.Contains(_id))
            {
                return false;
            }

            return true;
        }

        public static bool IsPlace(this Legacy _self, int _id)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return false;
            }

            var legacy_table = dc.LoadDataTable<DRLegacy>();
            var legacy_data = legacy_table.GetData(_id);
            if (legacy_data.IsPlace == 1)
            {
                return true;
            }

            return false;
        }

        public static bool IsCompose(this Legacy _self, int _id)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return false;
            }

            var legacy_table = dc.LoadDataTable<DRLegacy>();
            var legacy_data = legacy_table.GetData(_id);
            if (legacy_data.ComposeIDs[0] == -1)
            {
                return false;
            }

            return true;
        }

        public static bool IsMax(this Legacy _self, int _id)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return true;
            }

            var gData = dc.LoadDataTable<DRGlobalSettings>().GetData((int)eGlobalSetting.Legacy_Limit);
            if (_self.IsPlace(_id))
            {
                if (_self.LegacyPlaces.Count >= gData.Value1)
                {
                    return true;
                }
            }

            return false;
        }

        public static bool IsHave(this Legacy _self, int _id)
        {
            if (_self.LegacyPlaces.Contains(_id) || _self.LegacyUnPlaces.Contains(_id))
            {
                return true;
            }

            return false;
        }
    }
}